package xujiwen.transport.servlet;

import xujiwen.transport.bean.ChargeSTD;
import xujiwen.transport.bean.EnterRecord;
import xujiwen.transport.Mgr.EnterRecordMgr;
import xujiwen.transport.Mgr.ICCardMgr;
import xujiwen.transport.bean.PassRecord;
import xujiwen.transport.Mgr.PassRecordMgr;
import xujiwen.transport.Mgr.SDDisMgr;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import xujiwen.transport.util.locker.Password;

/**
 * Servlet implementation class for Servlet: Register
 * 
 */
public class AddPass extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public AddPass() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	@Override
	protected void doPost(final HttpServletRequest request,final HttpServletResponse response) throws ServletException,IOException {
		
		// 指明编码
		request.setCharacterEncoding("utf-8"); 
		
		//System.out.println(password + "\t" + email + "\t");
		try {
			EnterRecordMgr em = new EnterRecordMgr();
			PassRecordMgr pm = new PassRecordMgr();
			final String icid = request.getParameter("icid");

			final EnterRecord er = em.GetRecordByIC(icid);
			final PassRecord pr = new PassRecord();
			pr.setIcid(icid);
			pr.setCid(er.getCid());
			pr.setEntryName(er.getEntryName());
			pr.setEntryDate(er.getEnterDate());
			
			
			// 出口站
			final HttpSession session = request.getSession();
			String stationName = (String)session.getAttribute("station");
			pr.setExitName(stationName);
			SDDisMgr sm = new SDDisMgr();
			// 收费
			final HttpSession sessionstd = request.getSession();
			List<ChargeSTD> stdList = (List<ChargeSTD>)sessionstd.getAttribute("chargeStand");
			for(int i = 0; i < stdList.size(); ++i)
			{
				ChargeSTD std = stdList.get(i);
				if(std.getCarClass() == er.getCarClass())
				{
					pr.setDealSum(sm.GetDis(er.getEntryName(),stationName) * std.getPrice());
				}
			}
			// 判断IC余额是否足够
			ICCardMgr im = new ICCardMgr();
			if(im.IsRemaSumEnough(icid, pr.getDealSum()))
			{
				pm.Save(pr);
				System.out.println("添加出站记录成功");
			}
			else
			{
				pm.Save(pr);
				response.getWriter().println("IC余额不足！联系车主尽快充值。车牌号：" + pr.getCid());
				return ;
			}
		} catch(final NumberFormatException nfex){
			//response.sendRedirect("err.jsp");
			System.out.println("提交错误");			
			return ;
		}
		response.sendRedirect("operate/passRecord.jsp?searchStr=");
	}
}